package br.ufrj.cg.controller;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JOptionPane;

import br.ufrj.cg.model.StampImage;
import br.ufrj.cg.view.MainWindow;

public class MainWindowListener implements ActionListener{

	private MainWindow mainWindow;

	private MainWindowController mainWindowController;
	
	public MainWindowListener(MainWindow paramMainWindow, MainWindowController paramMainWindowController) {

		mainWindow = paramMainWindow;
		mainWindowController = paramMainWindowController;
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println("Button Clicked. Action Command: "
				+ e.getActionCommand());

		if (e.getActionCommand().equals("load")) {

			try {
				mainWindowController.openUploadDialog();
			} catch (IOException e1) {
				e1.printStackTrace();
			}

		}

		if (e.getActionCommand().equals("stamp")) {
			// TODO REMOVER
		}
		
		if (e.getActionCommand().equals("mirrorOnX")) {
			mainWindowController.mirrorPreviewOnX();
		}

		if (e.getActionCommand().equals("mirrorOnY")) {
			mainWindowController.mirrorPreviewOnY();
		}
		
		if (e.getActionCommand().equals("rotateBy90CCW"))
		{
			mainWindowController.rotatePreviewImageBy90Degrees(true);
		}
		
		if (e.getActionCommand().equals("rotateBy90ACW"))
		{
			mainWindowController.rotatePreviewImageBy90Degrees(false);
		}
		
		if (e.getActionCommand().equals("rotateBy180"))
		{
			mainWindowController.rotatePreviewImageBy180();
		}
		
		if (e.getActionCommand().equals("rotateBy270"))
		{
//			mainWindowController.rotatePreviewImageBy90Degrees(true);
		}
		
		if(e.getActionCommand().equals("invertColors"))
		{
			mainWindowController.invertPreviewColors();
		}
		
		if(e.getActionCommand().equals("toSepia"))
		{
			mainWindowController.turnPreviewToSepia();
		}
		
		if(e.getActionCommand().equals("toGrayScale"))
		{
			mainWindowController.turnPreviewToGrayScale();
		}
		
		if(e.getActionCommand().equals("colorizeToRed"))
		{
			mainWindowController.colorizePreview(Color.red);
		}
		
		if(e.getActionCommand().equals("colorizeToGreen"))
		{
			mainWindowController.colorizePreview(Color.green);
		}
		
		if(e.getActionCommand().equals("colorizeToBlue"))
		{
			mainWindowController.colorizePreview(Color.blue);
		}
		
		if(e.getActionCommand().equals("addTransparency"))
		{
			mainWindowController.addTransparency(50);
		}
		
		if (e.getActionCommand().equals("resize"))
		{
			mainWindowController.generatePreview(mainWindow.getCurrentStampImage().getImagePath());
			String newWidth = JOptionPane.showInputDialog("Define the new width:");
			String newHeight = JOptionPane.showInputDialog("Define the new height:");
			mainWindowController.resizePreviewImage(Integer.valueOf(newWidth), Integer.valueOf(newHeight));
		}
		
		if (e.getActionCommand().equals("resetImage")) {
			mainWindowController.resetImage();
		}

		if (e.getActionCommand().equals("save")) {
			mainWindowController.saveImage();
		}
		
		if (e.getActionCommand().equals("saveXML"))
		{
			mainWindowController.turnToPureColor();
//			mainWindowController.saveXML();
		}

		if(e.getActionCommand().equals("stampTopSecret"))
		{
			mainWindow.getMnImage().setEnabled(true);
			mainWindow.changeStampImage(new StampImage("stamps/topSecret.png"));
			mainWindowController.generatePreview(mainWindow.getCurrentStampImage().getImagePath());
		}
		
		if(e.getActionCommand().equals("stampSelo"))
		{
			mainWindow.getMnImage().setEnabled(true);
			mainWindow.changeStampImage(new StampImage("stamps/selo.png"));
			mainWindowController.generatePreview(mainWindow.getCurrentStampImage().getImagePath());
		
		}
		
		if(e.getActionCommand().equals("stampStar"))
		{
			mainWindow.getMnImage().setEnabled(true);
			mainWindow.changeStampImage(new StampImage("stamps/star.png"));
			mainWindowController.generatePreview(mainWindow.getCurrentStampImage().getImagePath());
		}
		
		if(e.getActionCommand().equals("stampUsPostage"))
		{
			mainWindow.getMnImage().setEnabled(true);
			mainWindow.changeStampImage(new StampImage("stamps/usPostage.png"));
			mainWindowController.generatePreview(mainWindow.getCurrentStampImage().getImagePath());
		}
		
		if(e.getActionCommand().equals("stampIronMan"))
		{
			mainWindow.getMnImage().setEnabled(true);
			mainWindow.changeStampImage(new StampImage("stamps/ironMan.png"));
			mainWindowController.generatePreview(mainWindow.getCurrentStampImage().getImagePath());
		}
		
		if(e.getActionCommand().equals("stampFleurDeLys"))
		{
			mainWindow.getMnImage().setEnabled(true);
			mainWindow.changeStampImage(new StampImage("stamps/fleurDeLys.png"));
			mainWindowController.generatePreview(mainWindow.getCurrentStampImage().getImagePath());
		}
	}


}
